{layout name="layouts/main" /}

<?php

use app\admin\model\field\system\UserToken;

$tableClass = new app\admin\extend\common\html2\Table();
echo $tableClass->addTitle($title);
echo $search ?? '';
echo $tableClass->table()->getTable();
$btn = [
    ['update','编辑'],
    ['del','删除'],
];
echo $tableClass->jsBtn('barDemo',$btn);
echo $tableClass->toolCheckbox('toolStatus', 'status', 'id', '启用|禁用');


?>


<script>
    //表格
    layui.use(['table'], function() {
        const table = layui.table
        const form  = layui.form
        const $ = layui.$
        const selfLayui = new SelfLayui(layui,$)

        //渲染
        const cols = [
            [
                {field:'user_id', title:'<?php echo UserToken::user_id ?>' },
                {field:'username', title:'<?php echo UserToken::username ?>' },
                {field:'token', title:'<?php echo UserToken::token ?>' },
                {field:'expiretime', title:'<?php echo UserToken::expiretime ?>' },
                
                {fixed: 'right', title:'操作', width:200, toolbar: '#barDemo'}
            ]
        ]
        const url = './getData'

        tableRender(cols,url)

        //搜索
        const active = {
            reload: function(){
                //执行重载
                const key = selfLayui.getSearch()
                selfLayui.reload(key)
            }
            ,del: function(){
                //通过复选框删除
                const url = './batchDel'
                selfLayui.delBatch(url)
            }
            ,add:function() {
                //添加
                selfLayui.openAdd('添加<?php echo $title;?>','./add')
            }
        }
        $('.demoTable .layui-btn').on('click', function(){
            const type = $(this).data('type')
            active[type] ? active[type].call(this) : ''
        })

        //监听状态开关
        form.on('switch(toolStatus)', function(data){
            const url = './status'
            selfLayui.changeStatus(url,data)
        })

        //监听工具条
        table.on('tool(test)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            const data = obj.data //获得当前行数据
            const layEvent = obj.event //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            if(layEvent === 'del'){
                //删除
                const url = './del?id='+obj.data.id
                selfLayui.del(url)
            } else if(layEvent === 'update'){
                //编辑
                selfLayui.openAdd('编辑<?php echo $title;?>','./update?id='+data.id)
            }
        })
    })
</script>

